using System;
using System.Collections;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Items;
using Server.Gumps;
using Server.Network;


namespace Server.Gumps
{
	public class BidAccountGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int HeaderGumpID = PropsConfig.HeaderGumpID;
		public static readonly int 	EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int PrevWidth = PropsConfig.PrevWidth;
		public static readonly int NextWidth = PropsConfig.NextWidth;

		public static readonly int PrevOffsetX = PropsConfig.PrevOffsetX, PrevOffsetY = PropsConfig.PrevOffsetY;
		public static readonly int PrevButtonID1 = PropsConfig.PrevButtonID1;
		public static readonly int PrevButtonID2 = PropsConfig.PrevButtonID2;

		public static readonly int NextOffsetX = PropsConfig.NextOffsetX, NextOffsetY = PropsConfig.NextOffsetY;
		public static readonly int NextButtonID1 = PropsConfig.NextButtonID1;
		public static readonly int NextButtonID2 = PropsConfig.NextButtonID2;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight + 2;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int HeaderTextHue = 777;
		private static readonly int WidthItem = 135;
		private static readonly int WidthVendor = 60;
		private static readonly int WidthBid = 180;
		private static readonly int WidthExpire = 150;
		private static readonly int WidthComment = 120;

		private static readonly int NameWidth = 45;
		private static readonly int ValueWidth = WidthItem + BorderSize + WidthVendor + BorderSize + WidthBid + BorderSize + WidthExpire + BorderSize + WidthComment;
		private static readonly int EntryCount = 20;

		private static readonly int TotalWidth = OffsetSize + NameWidth + OffsetSize + ValueWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (EntryCount + 1));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;

		private int m_Page;
		private List<BidRecord> m_list = new List<BidRecord>();

		private int m_btOffset = 20000;

		public BidAccountGump( Mobile from, int page ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_Page = page;

			if ( SaveStone.PlayerBids == null || !SaveStone.PlayerBids.ContainsKey( from ) )
				return;

			m_list = (List<BidRecord>)SaveStone.PlayerBids[from];

			if ( m_list == null || !( m_list.Count > 0 ) )
				return;

			Initialize( page );
		}

		private void Initialize( int page )
		{
			int count = m_list.Count - (page * EntryCount);

			if ( count < 0 )
				count = 0;
			else if ( count > EntryCount )
				count = EntryCount;

			int lastIndex = (page * EntryCount) + count - 1;

			if ( lastIndex >= 0 && lastIndex < m_list.Count && m_list[lastIndex] == null )
				--count;

			int totalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (count + 1));

			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, totalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			int emptyWidth = TotalWidth - PrevWidth - NextWidth - (OffsetSize * 4);
			AddImageTiled( x, y, PrevWidth, EntryHeight, HeaderGumpID );


			if ( page > 0 )
			{
				AddButton( x + PrevOffsetX, y + PrevOffsetY, PrevButtonID1, PrevButtonID2, 1, GumpButtonType.Reply, 0 );
			}

			x += PrevWidth + OffsetSize;
			AddImageTiled( x, y, emptyWidth, EntryHeight, HeaderGumpID );

			x += NameWidth - PrevWidth + OffsetSize;
			AddLabelCropped( x + TextOffsetX, y, WidthItem - TextOffsetX, EntryHeight, HeaderTextHue, "Item" );
			x += WidthItem + BorderSize;
			AddLabelCropped( x + TextOffsetX, y, WidthVendor - TextOffsetX, EntryHeight, HeaderTextHue, "Vendor" );
			x += WidthVendor + BorderSize;
			AddLabelCropped( x + TextOffsetX, y, WidthBid - TextOffsetX, EntryHeight, HeaderTextHue, "Bid" );
			x += WidthBid + BorderSize;
			AddLabelCropped( x + TextOffsetX, y, WidthExpire - TextOffsetX, EntryHeight, HeaderTextHue, "Expire" );
			x += WidthExpire + BorderSize;
			AddLabelCropped( x + TextOffsetX, y, WidthComment - TextOffsetX, EntryHeight, HeaderTextHue, "Comment" );
			x += WidthComment + OffsetSize;


//			x += emptyWidth + OffsetSize;
			AddImageTiled( x, y, NextWidth, EntryHeight, HeaderGumpID );

			if ( (page + 1) * EntryCount < m_list.Count )
			{
				AddButton( x + NextOffsetX, y + NextOffsetY, NextButtonID1, NextButtonID2, 2, GumpButtonType.Reply, 1 );
			}

			for ( int i = 0, index = page * EntryCount; i < count && index < m_list.Count; ++i, ++index )
			{
				x = BorderSize + OffsetSize;
				y += EntryHeight + OffsetSize;

				BidRecord br = m_list[index];
				string vname = "";

				int itemID = 0xEEF;		// Gold coins
				int hue = 0;
				bool dupe = false;

				if ( br.m_vendor != null && !br.m_vendor.Deleted && br.m_vendor is IHardsellable )
				{
					vname = br.m_vendor.Name;

					IHardsellable hs = br.m_vendor as IHardsellable;
					CurrencyInfo cinfo = hs.CInfo;

					if ( cinfo != null )
					{
						itemID = cinfo.m_itemID;
						hue = cinfo.m_hue;
						dupe = cinfo.m_dupeicon;
					}
				}


				AddImageTiled( x, y, NameWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, TextHue, "#" + ( i + 1 ).ToString() );
				x += NameWidth + OffsetSize;
				AddImageTiled( x, y, ValueWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, WidthItem - TextOffsetX, EntryHeight, TextHue, br.m_name );
				x += WidthItem + BorderSize;
				AddLabelCropped( x + TextOffsetX, y, WidthVendor - TextOffsetX, EntryHeight, TextHue, vname );
				x += WidthVendor + BorderSize;
				AddLabelCropped( x + TextOffsetX, y, WidthBid - TextOffsetX, EntryHeight, TextHue, br.m_bid.ToString() );

				AddItem( x + WidthBid - 60, y-2, itemID, hue );

				// Credit: Lord GreyWolf of RunUO, to add 5 to both x and y to display stackables
				if ( dupe )
					AddItem( x + WidthBid - 55, y+3, itemID, hue );

				x += WidthBid + BorderSize;
				AddLabelCropped( x + TextOffsetX, y, WidthExpire - TextOffsetX, EntryHeight, TextHue, br.m_expire.ToString( "dd/MMM/yyyy HH:mm" ) );
				x += WidthExpire + BorderSize;
				AddLabelCropped( x + TextOffsetX, y, WidthComment - TextOffsetX, EntryHeight, TextHue, br.m_comment );
				x += WidthComment + OffsetSize;


				if ( SetGumpID != 0 )
					AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

				AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + m_btOffset, GumpButtonType.Reply, 0 );

			}
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;
			
			switch ( info.ButtonID )
			{
				case 0: // Closed
				{
					break;
				}
				case 1: // Previous
				{
					if ( m_Page > 0 )
						from.SendGump( new BidAccountGump( from, m_Page - 1 ) );

					break;
				}
				case 2: // Next
				{
					if ( ( m_Page + 1 ) * EntryCount < m_list.Count )
						from.SendGump( new BidAccountGump( from, m_Page + 1 ) );

					break;
				}
				default:
				{
					int index = ( m_Page * EntryCount ) + ( info.ButtonID - m_btOffset );

					if ( index >= 0 && index < m_list.Count )
					{
						VendorManager.ToRemoveBidRecord( from, index );
					}

					break;
				}
			}
		}
	}
}